#include "debug_log.h"
#include <stdarg.h>
#include <fstream>
#include "GFunc.h"

char log_path[256] = "pp_log/";
void ADD_THREAD_LOG_COMMON(bool is_return, const char* str_thread, char const*	format,...)
{
    std::string long_log;
    long_log.clear();
    char log[1024]  = {0};
    char filename[256] = {0};
    sprintf(filename, "%slog_%s_debug.txt", log_path, str_thread);

    std::ofstream m_file;

    //BaseFile * p_file = GFunc::BaseFile_Create_File(filename, "a");
    m_file.open(filename, std::ios::app|std::ios::out);
    //if (p_file)
    if (m_file.is_open())
    {

        char* p_log = NULL;
        va_list ap;
        va_start(ap, format);
#ifdef WIN32
        int len = _vscprintf(format, ap);
#else
        int len = vsnprintf(NULL, 0, format, ap);
#endif 
        if (len > 1024)
        {
            long_log.resize(len);
            p_log = (char*)long_log.c_str();
            vsprintf(p_log, format, ap);
        }
        else
        {
            p_log = log;
            vsprintf(p_log, format, ap);
        }
        va_end(ap);


/*
        GFunc::BaseFile_Write(p_log, sizeof(char), len, p_file);
        if (is_return)
        {
            char r[1] = {'\n'};
            GFunc::BaseFile_Write(r, sizeof(char), 1, p_file);
        }
        */
        m_file << p_log;
        if (is_return)
        {
            m_file << '\n';
        }
        m_file.flush();
    }

    //GFunc::BaseFile_Close(p_file);
    m_file.close();
}